{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参考资料"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "中国科学院软件研究所 刘焕勇老师 https://github.com/liuhuanyong/QASystemOnMedicalKG"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "from py2neo import Graph, Node\n",
    "\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入数据集csv文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('medical_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(21, 23)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实体（节点）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有症状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "symptoms = []\n",
    "for each in df['症状']:\n",
    "    symptoms.extend(each.split(','))\n",
    "symptoms = set(symptoms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 所有科室"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "departments = []\n",
    "for each in df['科室']:\n",
    "    departments.extend(each.split(','))\n",
    "departments = set(departments)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "checks = []\n",
    "for each in df['检查']:\n",
    "    checks.extend(each.split(','))\n",
    "checks = set(checks)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "drugs = []\n",
    "for each in df['推荐药物']:\n",
    "    try:\n",
    "        drugs.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "for each in df['常用药物']:\n",
    "    try:\n",
    "        drugs.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "drugs = set(drugs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有食物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "foods = []\n",
    "for each in df['可以吃']:\n",
    "    try:\n",
    "        foods.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "for each in df['不可以吃']:\n",
    "    try:\n",
    "        foods.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "for each in df['推荐吃']:\n",
    "    try:\n",
    "        foods.extend(each.split(','))\n",
    "    except:\n",
    "        pass\n",
    "foods = set(foods)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实体：所有药物厂商"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "producers = []\n",
    "\n",
    "for each in df['具体药物']:\n",
    "    try:\n",
    "        for each_drug in each.split(','):\n",
    "            producer = each_drug.split('(')[0]\n",
    "            producers.append(producer)\n",
    "    except:\n",
    "        pass\n",
    "producers = set(producers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 疾病字典信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "disease_infos = [] # 疾病信息\n",
    "for idx, row in df.iterrows():\n",
    "    disease_infos.append(dict(row))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['疾病名称', '疾病描述', '疾病种类', '科室', '病因', '症状', '检查', '并发症', '花费', '疗程', '疗法', '治愈率', '易感人群', '感染概率', '感染途径', '预防措施', '推荐药物', '常用药物', '具体药物', '可以吃', '不可以吃', '推荐吃', '是否纳入医保'])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict(row).keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 关系（边）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def deduplicate(rels_old):\n",
    "    '''关系去重函数'''\n",
    "    rels_new = []\n",
    "    for each in rels_old:\n",
    "        if each not in rels_new:\n",
    "            rels_new.append(each)\n",
    "    return rels_new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_check = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['检查'].split(','):\n",
    "        rels_check.append([row['疾病名称'], each])\n",
    "rels_check = deduplicate(rels_check)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '胸部CT检查'],\n",
       " ['肺泡蛋白质沉积症', '肺活检'],\n",
       " ['肺泡蛋白质沉积症', '支气管镜检查'],\n",
       " ['百日咳', '耳、鼻、咽拭子细菌培养'],\n",
       " ['百日咳', '周围血白细胞计数及分类检验'],\n",
       " ['百日咳', '血常规'],\n",
       " ['百日咳', '酶联免疫吸附试验'],\n",
       " ['百日咳', '白细胞分类计数'],\n",
       " ['苯中毒', '血常规'],\n",
       " ['苯中毒', '骨髓象分析'],\n",
       " ['苯中毒', '先令氏指数'],\n",
       " ['喘息样支气管炎', '肺部检查'],\n",
       " ['喘息样支气管炎', '肺和胸膜听诊'],\n",
       " ['喘息样支气管炎', '抗链球菌型M蛋白抗体'],\n",
       " ['喘息样支气管炎', '抗链球菌壁多糖抗体'],\n",
       " ['喘息样支气管炎', '酶联免疫吸附试验'],\n",
       " ['成人呼吸窘迫综合征', '胸部CT检查'],\n",
       " ['成人呼吸窘迫综合征', '呼吸肌功能测定'],\n",
       " ['成人呼吸窘迫综合征', '血浆蛋白C抗原'],\n",
       " ['成人呼吸窘迫综合征', '肺泡气-动脉血氧分压差'],\n",
       " ['成人呼吸窘迫综合征', '肺毛细血管楔压'],\n",
       " ['大量羊水吸入', '肺部检查'],\n",
       " ['大量羊水吸入', '胸部透视'],\n",
       " ['大量羊水吸入', '胸部平片'],\n",
       " ['大量羊水吸入', '胸部CT检查'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '痰液中细胞分类'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胸部平片'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '痰液中寄生虫和虫卵'],\n",
       " ['大叶性肺炎', 'Optochin敏感试验'],\n",
       " ['大叶性肺炎', '小白鼠毒力试验'],\n",
       " ['大叶性肺炎', '痰培养'],\n",
       " ['大叶性肺炎', '肺活量体重指数'],\n",
       " ['大叶性肺炎', '胸部平片'],\n",
       " ['大叶性肺炎', '免疫电泳'],\n",
       " ['大叶性肺炎', '血常规'],\n",
       " ['大叶性肺炎', '痰液细菌涂片检查'],\n",
       " ['大楼病综合征', '钼靶X线检查'],\n",
       " ['大楼病综合征', 'CT检查'],\n",
       " ['大楼病综合征', '血常规'],\n",
       " ['二硫化碳中毒', '血常规'],\n",
       " ['二硫化碳中毒', '尿常规'],\n",
       " ['二硫化碳中毒', '肾功能检查'],\n",
       " ['二硫化碳中毒', '神经系统检查'],\n",
       " ['二硫化碳中毒', '眼底荧光血管造影'],\n",
       " ['二硫化碳中毒', '肌电图'],\n",
       " ['二硫化碳中毒', '脑电图检查'],\n",
       " ['二硫化碳中毒', '心肺功能运动试验（CPET）'],\n",
       " ['肺-胸膜阿米巴病', '胸部平片'],\n",
       " ['肺-胸膜阿米巴病', '痰液病原体检查'],\n",
       " ['肺出血－肾炎综合征', '痰液常规检查'],\n",
       " ['肺出血－肾炎综合征', '尿液镜检法'],\n",
       " ['肺出血－肾炎综合征', '胸部平片'],\n",
       " ['肺出血－肾炎综合征', '肾功能检查'],\n",
       " ['肺出血－肾炎综合征', '抗肾小球基底膜抗体测定（AGBM）'],\n",
       " ['肺出血－肾炎综合征', '隐血试验与含铁血黄素检查'],\n",
       " ['肺放线菌病', '痰液病原体检查'],\n",
       " ['肺放线菌病', '胸部平片'],\n",
       " ['肺泡蛋白沉着症', '胸部CT检查'],\n",
       " ['肺泡蛋白沉着症', '支气管镜检查'],\n",
       " ['肺泡蛋白沉着症', '肺活检'],\n",
       " ['肺曲菌病', '胸部平片'],\n",
       " ['肺曲菌病', '痰培养'],\n",
       " ['肺曲菌病', '纤维支气管镜检查'],\n",
       " ['肺曲菌病', '胸部CT检查'],\n",
       " ['肺曲菌病', '痰液细菌涂片检查'],\n",
       " ['放射性肺炎', '胸部平片'],\n",
       " ['放射性肺炎', '肺功能检查'],\n",
       " ['放射性肺炎', '通气与血流灌注比值（V/Q）'],\n",
       " ['放射性肺炎', '每分钟最大通气量（MVV）'],\n",
       " ['放射性肺炎', '血常规'],\n",
       " ['肺念珠菌病', '肺量计检测'],\n",
       " ['肺念珠菌病', '胸部平片'],\n",
       " ['肺念珠菌病', '痰液细菌涂片检查'],\n",
       " ['肺大疱', '胸部CT检查'],\n",
       " ['肺大疱', '支气管造影'],\n",
       " ['肺大疱', '胸部平片'],\n",
       " ['肺炎球菌肺炎', '胸部平片'],\n",
       " ['肺炎球菌肺炎', '血常规'],\n",
       " ['肺气肿', '肺量计检测'],\n",
       " ['肺气肿', '残气量／肺总量比值（RV/TLC）'],\n",
       " ['肺气肿', '肺容量测定'],\n",
       " ['肺气肿', '肺活量（VC）'],\n",
       " ['肺气肿', '最大呼气流量-容积曲线（MEFV）'],\n",
       " ['肺气肿', '深吸气量（IC）'],\n",
       " ['肺气肿', '一秒用力呼出量／用力肺活量比值'],\n",
       " ['肺气肿', '心电图'],\n",
       " ['肺气肿', '无效腔气量／潮气量比值'],\n",
       " ['肺气肿', '肺泡气-动脉血氧分压差'],\n",
       " ['肺炎杆菌肺炎', '痰液细菌涂片检查'],\n",
       " ['肺炎杆菌肺炎', '痰液细菌培养'],\n",
       " ['肺炎杆菌肺炎', '血常规']]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_check"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-症状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_symptom = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['症状'].split(','):\n",
    "        rels_symptom.append([row['疾病名称'], each])\n",
    "rels_symptom = deduplicate(rels_symptom)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '紫绀'],\n",
       " ['肺泡蛋白质沉积症', '胸痛'],\n",
       " ['肺泡蛋白质沉积症', '呼吸困难'],\n",
       " ['肺泡蛋白质沉积症', '乏力'],\n",
       " ['肺泡蛋白质沉积症', '毓卓'],\n",
       " ['百日咳', '吸气时有蝉鸣音'],\n",
       " ['百日咳', '痉挛性咳嗽'],\n",
       " ['百日咳', '胸闷'],\n",
       " ['百日咳', '肺阴虚'],\n",
       " ['百日咳', '抽搐'],\n",
       " ['百日咳', '低热'],\n",
       " ['百日咳', '闫鹏辉'],\n",
       " ['百日咳', '惊厥'],\n",
       " ['苯中毒', '恶心'],\n",
       " ['苯中毒', '抽搐'],\n",
       " ['苯中毒', '感觉障碍'],\n",
       " ['喘息样支气管炎', '耸肩喘息'],\n",
       " ['喘息样支气管炎', '哮鸣音'],\n",
       " ['喘息样支气管炎', '纤毛上皮细胞损伤脱落'],\n",
       " ['喘息样支气管炎', '变应性咳嗽'],\n",
       " ['喘息样支气管炎', '化学性支气管炎'],\n",
       " ['喘息样支气管炎', '喘息'],\n",
       " ['喘息样支气管炎', '冬春季的慢性咳...'],\n",
       " ['喘息样支气管炎', '咳嗽伴哮鸣音'],\n",
       " ['成人呼吸窘迫综合征', '呼吸困难'],\n",
       " ['成人呼吸窘迫综合征', '紫绀'],\n",
       " ['成人呼吸窘迫综合征', '心源性呼吸窘迫'],\n",
       " ['大量羊水吸入', '面色青紫'],\n",
       " ['大量羊水吸入', '呼吸困难'],\n",
       " ['大量羊水吸入', '口唇青紫'],\n",
       " ['大量羊水吸入', '肺纹理增粗'],\n",
       " ['大量羊水吸入', '出生后即有持续青紫'],\n",
       " ['大量羊水吸入', '口唇和甲床略带青紫'],\n",
       " ['大量羊水吸入', '气急'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '嗜酸性粒细胞增多'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '咽部异物感'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胸闷憋气'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '咯血伴发热'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胸闷'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '乏力'],\n",
       " ['大叶性肺炎', '湿啰音'],\n",
       " ['大叶性肺炎', '胸痛'],\n",
       " ['大叶性肺炎', '发烧'],\n",
       " ['大叶性肺炎', '咳铁锈色痰'],\n",
       " ['大叶性肺炎', '急性面容'],\n",
       " ['大叶性肺炎', '呼吸音减弱'],\n",
       " ['大楼病综合征', '紧张性头晕'],\n",
       " ['大楼病综合征', '紧张性头痛'],\n",
       " ['大楼病综合征', '间歇性头晕'],\n",
       " ['大楼病综合征', '鼻塞'],\n",
       " ['大楼病综合征', '头晕'],\n",
       " ['大楼病综合征', '恶心'],\n",
       " ['大楼病综合征', '眼睛痒'],\n",
       " ['大楼病综合征', '目赤'],\n",
       " ['大楼病综合征', '间歇性头痛'],\n",
       " ['二硫化碳中毒', '昏迷'],\n",
       " ['二硫化碳中毒', '腱反射消失'],\n",
       " ['二硫化碳中毒', '呕吐'],\n",
       " ['二硫化碳中毒', '谵妄'],\n",
       " ['二硫化碳中毒', '多发性神经炎'],\n",
       " ['二硫化碳中毒', '浅感觉减退或缺失'],\n",
       " ['二硫化碳中毒', '恶心'],\n",
       " ['二硫化碳中毒', '感觉障碍'],\n",
       " ['肺-胸膜阿米巴病', '盗汗'],\n",
       " ['肺-胸膜阿米巴病', '腹泻'],\n",
       " ['肺-胸膜阿米巴病', '乏力'],\n",
       " ['肺-胸膜阿米巴病', '发热伴咳嗽、咯...'],\n",
       " ['肺出血－肾炎综合征', '肺部出血'],\n",
       " ['肺出血－肾炎综合征', '呼吸困难'],\n",
       " ['肺出血－肾炎综合征', '咯血'],\n",
       " ['肺出血－肾炎综合征', '气短'],\n",
       " ['肺出血－肾炎综合征', '发热伴咳嗽、咯...'],\n",
       " ['肺出血－肾炎综合征', '蛋白尿'],\n",
       " ['肺放线菌病', '骨膜炎'],\n",
       " ['肺放线菌病', '体重减轻'],\n",
       " ['肺放线菌病', '咯血'],\n",
       " ['肺放线菌病', '盗汗'],\n",
       " ['肺放线菌病', '乏力'],\n",
       " ['肺放线菌病', '低热'],\n",
       " ['肺泡蛋白沉着症', '胸痛'],\n",
       " ['肺泡蛋白沉着症', '紫绀'],\n",
       " ['肺泡蛋白沉着症', '呼吸困难'],\n",
       " ['肺泡蛋白沉着症', '肺泡炎症'],\n",
       " ['肺泡蛋白沉着症', '肺泡灌洗液可见...'],\n",
       " ['肺泡蛋白沉着症', '乏力'],\n",
       " ['肺泡蛋白沉着症', '发热伴咳嗽、咯...'],\n",
       " ['肺曲菌病', '咳出棕色痰栓'],\n",
       " ['肺曲菌病', '嗜酸性粒细胞增多'],\n",
       " ['肺曲菌病', '胸痛'],\n",
       " ['放射性肺炎', '闫铁'],\n",
       " ['放射性肺炎', '肺纤维化'],\n",
       " ['放射性肺炎', '胸痛'],\n",
       " ['放射性肺炎', '低热'],\n",
       " ['肺念珠菌病', '痰呈粘液脓性'],\n",
       " ['肺念珠菌病', '肺部啰音'],\n",
       " ['肺大疱', '胸闷'],\n",
       " ['肺大疱', '气短'],\n",
       " ['肺大疱', '胸闷憋气'],\n",
       " ['肺大疱', '发热伴咳嗽、咯...'],\n",
       " ['肺炎球菌肺炎', '痰中带血丝'],\n",
       " ['肺炎球菌肺炎', '咳嗽伴胸痛'],\n",
       " ['肺炎球菌肺炎', '胸痛'],\n",
       " ['肺炎球菌肺炎', '发热伴咳嗽、咯...'],\n",
       " ['肺气肿', '纵隔浊音界扩大'],\n",
       " ['肺气肿', '桶状胸'],\n",
       " ['肺气肿', '小支气管粘膜水肿'],\n",
       " ['肺气肿', '黏稠或脓性痰伴...'],\n",
       " ['肺气肿', '横膈低平'],\n",
       " ['肺气肿', '胸闷'],\n",
       " ['肺气肿', '呼气音延长'],\n",
       " ['肺气肿', '肺部啰音'],\n",
       " ['肺气肿', '呼吸音减弱'],\n",
       " ['肺炎杆菌肺炎', '畏寒'],\n",
       " ['肺炎杆菌肺炎', '胸痛'],\n",
       " ['肺炎杆菌肺炎', '气急'],\n",
       " ['肺炎杆菌肺炎', '发热伴咳嗽、咯...']]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_symptom"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-疾病（并发症）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "rels_acompany = []\n",
    "for idx, row in df.iterrows():\n",
    "    for each in row['并发症'].split(','):\n",
    "        rels_acompany.append([row['疾病名称'], each])\n",
    "rels_acompany = deduplicate(rels_acompany)\n",
    "print(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '多重肺部感染'],\n",
       " ['百日咳', '肺不张'],\n",
       " ['苯中毒', '贫血'],\n",
       " ['喘息样支气管炎', '支气管哮喘'],\n",
       " ['成人呼吸窘迫综合征', '细菌性肺炎'],\n",
       " ['大量羊水吸入', '呼吸衰竭'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '胆道蛔虫病'],\n",
       " ['大叶性肺炎', '脓胸'],\n",
       " ['大楼病综合征', '抑郁症'],\n",
       " ['二硫化碳中毒', '昏迷'],\n",
       " ['肺-胸膜阿米巴病', '阿米巴肝脓肿'],\n",
       " ['肺出血－肾炎综合征', '便血'],\n",
       " ['肺放线菌病', '膈下脓肿'],\n",
       " ['肺泡蛋白沉着症', '呼吸衰竭'],\n",
       " ['肺曲菌病', '过敏性鼻炎'],\n",
       " ['放射性肺炎', '肺气肿'],\n",
       " ['肺念珠菌病', '菌血症'],\n",
       " ['肺大疱', '张力性气胸'],\n",
       " ['肺炎球菌肺炎', '败血症'],\n",
       " ['肺气肿', '呼吸衰竭'],\n",
       " ['肺气肿', '自发性气胸'],\n",
       " ['肺气肿', '慢性肺源性心脏病'],\n",
       " ['肺气肿', '胃溃疡'],\n",
       " ['肺炎杆菌肺炎', '脑疝']]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_acompany"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-推荐药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_recommanddrug = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['推荐药物'].split(','):\n",
    "            rels_recommanddrug.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_recommanddrug = deduplicate(rels_recommanddrug)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-常用药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_commonddrug = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['常用药物'].split(','):\n",
    "            rels_commonddrug.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_commonddrug = deduplicate(rels_commonddrug)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-不可以吃"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_noteat = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['不可以吃'].split(','):\n",
    "            rels_noteat.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_noteat = deduplicate(rels_noteat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-可以吃"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_doeat = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['可以吃'].split(','):\n",
    "            rels_doeat.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_doeat = deduplicate(rels_doeat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-推荐吃"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_recommandeat = []\n",
    "for idx, row in df.iterrows():\n",
    "    try:\n",
    "        for each in row['推荐吃'].split(','):\n",
    "            rels_recommandeat.append([row['疾病名称'], each])\n",
    "    except:\n",
    "        pass\n",
    "rels_recommandeat = deduplicate(rels_recommandeat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：药物厂商-具体药物"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_drug_producer = []\n",
    "for each in df['具体药物']:\n",
    "    try:\n",
    "        for each_drug in each.split(','):\n",
    "            producer = each_drug.split('(')[0]\n",
    "            drug = each_drug.split('(')[1][:-1]\n",
    "            rels_drug_producer.append([producer, drug])\n",
    "    except:\n",
    "        pass\n",
    "rels_drug_producer = deduplicate(rels_drug_producer)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 关系：疾病-科室、小科室-大科室"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "rels_category = [] # 关系：疾病-科室\n",
    "rels_department = [] # 关系：小科室-大科室\n",
    "for idx, row in df.iterrows():\n",
    "    if len(row['科室'].split(',')) == 1:\n",
    "        rels_category.append([row['疾病名称'], row['科室']])\n",
    "    else:\n",
    "        big = row['科室'].split(',')[0] # 大科室\n",
    "        small = row['科室'].split(',')[1] # 小科室\n",
    "        rels_category.append([row['疾病名称'], small])\n",
    "        rels_department.append([small, big])\n",
    "rels_category = deduplicate(rels_category)\n",
    "rels_department = deduplicate(rels_department)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['肺泡蛋白质沉积症', '呼吸内科'],\n",
       " ['百日咳', '小儿内科'],\n",
       " ['苯中毒', '急诊科'],\n",
       " ['喘息样支气管炎', '呼吸内科'],\n",
       " ['成人呼吸窘迫综合征', '呼吸内科'],\n",
       " ['大量羊水吸入', '小儿内科'],\n",
       " ['单纯性肺嗜酸粒细胞浸润症', '呼吸内科'],\n",
       " ['大叶性肺炎', '呼吸内科'],\n",
       " ['大楼病综合征', '其他综合'],\n",
       " ['二硫化碳中毒', '急诊科'],\n",
       " ['肺-胸膜阿米巴病', '呼吸内科'],\n",
       " ['肺出血－肾炎综合征', '呼吸内科'],\n",
       " ['肺放线菌病', '呼吸内科'],\n",
       " ['肺泡蛋白沉着症', '呼吸内科'],\n",
       " ['肺曲菌病', '呼吸内科'],\n",
       " ['放射性肺炎', '呼吸内科'],\n",
       " ['肺念珠菌病', '呼吸内科'],\n",
       " ['肺大疱', '呼吸内科'],\n",
       " ['肺炎球菌肺炎', '呼吸内科'],\n",
       " ['肺气肿', '呼吸内科'],\n",
       " ['肺炎杆菌肺炎', '呼吸内科']]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_category"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['呼吸内科', '内科'], ['小儿内科', '儿科'], ['其他综合', '其他科室']]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rels_department"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 连接图数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'neo4j_version'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36macquire\u001b[1;34m(self, force_reset, can_overfill)\u001b[0m\n\u001b[0;32m    805\u001b[0m                 \u001b[1;31m# Plan A: select a free connection from the pool\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 806\u001b[1;33m                 \u001b[0mcx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_free_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpopleft\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    807\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[0mIndexError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: pop from an empty deque",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mC:\\AppData\\Local\\Temp/ipykernel_82660/2004455874.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 注意，这里的用户名为neo4j全局用户名，而非DBMS或者database的名称\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'http://localhost:7474'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mauth\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'neo4j'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'ABC123'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\database.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, profile, name, **settings)\u001b[0m\n\u001b[0;32m    286\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    287\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprofile\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0msettings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 288\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mservice\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mGraphService\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprofile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0msettings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    289\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    290\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mschema\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mSchema\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\database.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, profile, **settings)\u001b[0m\n\u001b[0;32m    117\u001b[0m             \u001b[1;31m# Ensures credentials are checked on construction\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    118\u001b[0m             \u001b[0mconnector_settings\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"init_size\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 119\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_connector\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mConnector\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprofile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mconnector_settings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    120\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_graphs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    121\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, profile, user_agent, init_size, max_size, max_age, routing_refresh_ttl)\u001b[0m\n\u001b[0;32m    958\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    959\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_router\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 960\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_add_pools\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_initial_routers\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    961\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    962\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__repr__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m_add_pools\u001b[1;34m(self, *profiles)\u001b[0m\n\u001b[0;32m    980\u001b[0m                 \u001b[1;32mcontinue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    981\u001b[0m             \u001b[0mlog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Adding connection pool for profile %r\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprofile\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 982\u001b[1;33m             pool = ConnectionPool.open(\n\u001b[0m\u001b[0;32m    983\u001b[0m                 \u001b[0mprofile\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    984\u001b[0m                 \u001b[0muser_agent\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_user_agent\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36mopen\u001b[1;34m(cls, profile, user_agent, init_size, max_size, max_age, on_broken)\u001b[0m\n\u001b[0;32m    647\u001b[0m         \"\"\"\n\u001b[0;32m    648\u001b[0m         \u001b[0mpool\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprofile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muser_agent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmax_size\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmax_age\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon_broken\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 649\u001b[1;33m         \u001b[0mseeds\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mpool\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minit_size\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdefault_init_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    650\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mseed\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mseeds\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    651\u001b[0m             \u001b[0mseed\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelease\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m    647\u001b[0m         \"\"\"\n\u001b[0;32m    648\u001b[0m         \u001b[0mpool\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprofile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0muser_agent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmax_size\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmax_age\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon_broken\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 649\u001b[1;33m         \u001b[0mseeds\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mpool\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0m_\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minit_size\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdefault_init_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    650\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mseed\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mseeds\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    651\u001b[0m             \u001b[0mseed\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelease\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36macquire\u001b[1;34m(self, force_reset, can_overfill)\u001b[0m\n\u001b[0;32m    811\u001b[0m                     \u001b[1;31m# ConnectionUnavailable exception, which\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    812\u001b[0m                     \u001b[1;31m# should bubble up to the caller.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 813\u001b[1;33m                     \u001b[0mcx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_connect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    814\u001b[0m                     \u001b[1;32mif\u001b[0m \u001b[0mcx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msupports_multi\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    815\u001b[0m                         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_supports_multi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m_connect\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    762\u001b[0m         \"\"\" Open and return a new connection.\n\u001b[0;32m    763\u001b[0m         \"\"\"\n\u001b[1;32m--> 764\u001b[1;33m         cx = Connection.open(self.profile, user_agent=self.user_agent,\n\u001b[0m\u001b[0;32m    765\u001b[0m                              \u001b[0mon_release\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelease\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    766\u001b[0m                              on_broken=lambda msg: self.__on_broken(msg))\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36mopen\u001b[1;34m(cls, profile, user_agent, on_release, on_broken)\u001b[0m\n\u001b[0;32m    176\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0mprofile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprotocol\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"http\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    177\u001b[0m             \u001b[1;32mfrom\u001b[0m \u001b[0mpy2neo\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mclient\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhttp\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mHTTP\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 178\u001b[1;33m             return HTTP.open(profile, user_agent=user_agent,\n\u001b[0m\u001b[0;32m    179\u001b[0m                              on_release=on_release, on_broken=on_broken)\n\u001b[0;32m    180\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\http.py\u001b[0m in \u001b[0;36mopen\u001b[1;34m(cls, profile, user_agent, on_release, on_broken)\u001b[0m\n\u001b[0;32m     61\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     62\u001b[0m             \u001b[0mhttp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprofile\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon_release\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mon_release\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 63\u001b[1;33m             \u001b[0mhttp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_hello\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0muser_agent\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mhttp_user_agent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     64\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mhttp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     65\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mHTTPError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0merror\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Pytorch\\Anaconda\\lib\\site-packages\\py2neo\\client\\http.py\u001b[0m in \u001b[0;36m_hello\u001b[1;34m(self, user_agent)\u001b[0m\n\u001b[0;32m    148\u001b[0m             \u001b[1;31m#   \"neo4j_version\" : \"3.5.12\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    149\u001b[0m             \u001b[1;31m# }\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 150\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_neo4j_version\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mVersion\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmetadata\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"neo4j_version\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# Neo4j 3.x\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    151\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mserver_agent\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Neo4j/{}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_neo4j_version\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    152\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'neo4j_version'"
     ]
    }
   ],
   "source": [
    "# 注意，这里的用户名为neo4j全局用户名，而非DBMS或者database的名称\n",
    "g = Graph('http://localhost:7474', auth=('neo4j', '123456'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建知识图谱实体（节点）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 样例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Neo4j样例代码\n",
    "\n",
    "# 创建实体\n",
    "# node = Node('Disease', name='百日咳', easy_get='多见于小儿')\n",
    "# g.create(node)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除所有实体和关系\n",
    "cypher = 'MATCH (n) DETACH DELETE n'\n",
    "g.run(cypher)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建疾病实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "for disease_dict in disease_infos:\n",
    "    try:\n",
    "        node = Node(\"Disease\",\n",
    "                    name=disease_dict['疾病名称'],\n",
    "                    desc=disease_dict['疾病描述'],\n",
    "                    prevent=disease_dict['预防措施'],\n",
    "                    cause=disease_dict['病因'],\n",
    "                    easy_get=disease_dict['易感人群'],\n",
    "                    cure_lasttime=disease_dict['疗程'],\n",
    "                    cure_department=disease_dict['科室'],\n",
    "                    cure_way=disease_dict['疗法'], \n",
    "                    cured_prob=disease_dict['治愈率'])\n",
    "        g.create(node)\n",
    "        count += 1\n",
    "        print('创建疾病实体：', disease_dict['疾病名称'])\n",
    "    except:\n",
    "        pass\n",
    "print('共创建 {} 个疾病实体'.format(count))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建药物实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for each in drugs:\n",
    "    node = Node('Drug', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建食物实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for each in foods:\n",
    "    node = Node('Food', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建检查实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for each in checks:\n",
    "    node = Node('Check', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建科室实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for each in departments:\n",
    "    node = Node('Department', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建 药物厂商 实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for each in producers:\n",
    "    node = Node('Producer', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建 症状 实体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for each in symptoms:\n",
    "    node = Node('Symptom', name=each)\n",
    "    g.create(node)\n",
    "    print('创建实体 {}'.format(each))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建知识图谱关系（连接、边）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 样例代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Neo4j样例代码\n",
    "\n",
    "# # 删除所有实体和关系\n",
    "# cypher = 'MATCH (n) DETACH DELETE n'\n",
    "# g.run(cypher)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 创建关系 样例代码\n",
    "# start_node = 'Disease'\n",
    "# end_node = 'Check'\n",
    "# p = '百日咳'\n",
    "# q = '血常规'\n",
    "# rel_type = 'need_check'\n",
    "# rel_name = '诊断检查'\n",
    "\n",
    "# # Cypher语句\n",
    "# query = \"match(p:%s),(q:%s) where p.name='%s' and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)\" % (start_node, end_node, p, q, rel_type, rel_name)\n",
    "# print(query)\n",
    "# g.run(query) # 运行 Cypher 语句\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_relationship(start_node, end_node, edges, rel_type, rel_name):\n",
    "    '''创建关系函数'''\n",
    "    for edge in edges:\n",
    "        p = edge[0]\n",
    "        q = edge[1]\n",
    "        # 创建关系的 Cypher 语句\n",
    "        query = \"match(p:%s),(q:%s) where p.name='%s' and q.name='%s' create (p)-[rel:%s{name:'%s'}]->(q)\" % (start_node, end_node, p, q, rel_type, rel_name)\n",
    "        try:\n",
    "            g.run(query) # 运行 Cypher 语句\n",
    "            print('创建关系 {}-{}->{}'.format(p, rel_type, q))\n",
    "        except Exception as e:\n",
    "            print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建所有关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "create_relationship('Disease', 'Food', rels_recommandeat, 'recommand_eat', '推荐食谱')\n",
    "create_relationship('Disease', 'Food', rels_noteat, 'no_eat', '忌吃')\n",
    "create_relationship('Disease', 'Food', rels_doeat, 'do_eat', '宜吃')\n",
    "create_relationship('Department', 'Department', rels_department, 'belongs_to', '属于')\n",
    "create_relationship('Disease', 'Drug', rels_commonddrug, 'common_drug', '常用药品')\n",
    "create_relationship('Producer', 'Drug', rels_drug_producer, 'drugs_of', '生产药品')\n",
    "create_relationship('Disease', 'Drug', rels_recommanddrug, 'recommand_drug', '好评药品')\n",
    "create_relationship('Disease', 'Check', rels_check, 'need_check', '诊断检查')\n",
    "create_relationship('Disease', 'Symptom', rels_symptom, 'has_symptom', '症状')\n",
    "create_relationship('Disease', 'Disease', rels_acompany, 'acompany_with', '并发症')\n",
    "create_relationship('Disease', 'Department', rels_category, 'belongs_to', '所属科室')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
